import java.util.List;

// 1、无头单向非循环链表实现
public class SingleLinkedList {
    //头插法
    public class ListNode{
        int val;
        ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    ListNode head = null;

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            return;
        }
        ListNode cur = head;

        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data){
        ListNode cur = head;
        ListNode node =  new ListNode(data);
        if(index < 0 || index > size()-1){
            return false;
        } else if (index == 0) {
            node.next = head;
            head = node;
        } else{
            while(index-1 > 0){
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
            cur.next = node;
        }
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head == null){
            return false;
        }
        ListNode cur = head;
        while(cur!=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }

        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        if(head == null){
            return;
        }

        while (cur.next != null) {
            if(cur.next.val == key){
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
        if(cur.val == key){
            cur = null;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        if(head.next == null){
            if(head.val == key){
                head = null;
            }
            return;
        }
        if(head.val == key){
            head = head.next;
        }
        ListNode cur = head.next;
        while (cur.next != null) {
            if(cur.next.val == key){
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }
        if(cur.val == key){
            cur = null;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int len = 0;
        while(cur != null){
            cur = cur.next;
            len++;
        }
        return len;
    }
    public void display(){
        if(head == null){
            return;
        }
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void clear(){
        head = null;
    }
}